package com.whccb.apps.office.document.event.receive;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class ReceiveFileDoAfterProcessCompleteEvent extends ExecuteListener
		implements ExecuteListenerInterface {
	public String getDescription() {
		return "收文办理部门经理签批完成后，将留言写到会签栏.";
	}

	public String getProvider() {
		return "ActionSoft";
	}

	public String getVersion() {
		return "AWS 6.0";
	}

	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		String subBindid = pec.getProcessInstance().getId();
		String parentBindid = pec.getProcessInstance().getParentProcessInstId();
		List<Map<String, String>> checkList = getCheckList(subBindid, pec
				.getTaskInstance().getId(), pec.getUserContext().getUID());
		String taskMsg = "";
		String createTime = "";
		if (checkList != null && !checkList.isEmpty() && checkList.size() > 0) {
			Map<String, String> checkMap = checkList.get(0);
			if (checkMap != null && !checkMap.isEmpty()) {
				taskMsg = checkMap.get("MSG");
				String createDateTime = checkMap.get("CREATEDATE");
				if (StringUtils.isNotEmpty(createDateTime)
						&& createDateTime.contains(".")) {
					createTime = createDateTime.substring(0,
							createDateTime.lastIndexOf("."));
				}
			}

		}

		String oldValue = SDK.getBOAPI()
				.getByProcess("BO_WHBK_OD_RECEIVE_DO", subBindid, "HQ")
				.toString();
		if (taskMsg != null && StringUtils.isNotEmpty(taskMsg.trim())) {
			String rightTaskMsg = getTaskMsg(pec.getUserContext(), oldValue,
					taskMsg, createTime);
			if (rightTaskMsg != null && StringUtils.isNotEmpty(rightTaskMsg)) {
				// 查询所有子流程
				List<ProcessInstance> subProcessList = SDK.getProcessAPI()
						.query().parentProcessInstId(parentBindid).list();
				if (subProcessList != null && !subProcessList.isEmpty()
						&& subProcessList.size() > 0) {
					for (ProcessInstance subProcess : subProcessList) {
						if (subProcess != null) {
							// 更新子流程
							SDK.getBOAPI().updateByBindId(
									"BO_WHBK_OD_RECEIVE_DO",
									subProcess.getId(), "HQ", rightTaskMsg);
						}
					}
				}
				// 更新主流程
				SDK.getBOAPI().updateByBindId("BO_WHBK_OD_RECEIVE",
						parentBindid, "HQ", rightTaskMsg);
			}
		}

	}

	private List<Map<String, String>> getCheckList(String bindid,
			String taskid, String uid) {
		String querySql = " SELECT a.CREATEUSER,a.CREATEDATE,a.ACTIONNAME,a.MSG FROM WFC_COMMENT a WHERE a.PROCESSINSTID = :PROCESSINSTID AND a.TASKINSTID = :TASKINSTID AND a.CREATEUSER = :CREATEUSER";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("PROCESSINSTID", bindid);
		paramMap.put("TASKINSTID", taskid);
		paramMap.put("CREATEUSER", uid);
		return DBSql.query(querySql, paramMap,
				new RowMapper<Map<String, String>>() {
					public Map<String, String> mapRow(ResultSet rs, int index)
							throws SQLException {
						Map<String, String> map = new HashMap<String, String>();
						map.put("CREATEUSER", rs.getString("CREATEUSER"));
						map.put("CREATEDATE", rs.getString("CREATEDATE"));
						map.put("ACTIONNAME", rs.getString("ACTIONNAME"));
						map.put("MSG", rs.getString("MSG"));
						return map;
					}

				});
	}

	private String getTaskMsg(UserContext me, String oldValue, String msg,
			String createTime) {
		StringBuffer result = new StringBuffer();
		if (oldValue != null && StringUtils.isNotEmpty(oldValue)) {
			result.append(oldValue);
			result.append("\r\n").append("\r\n");
		}
		String userName = me.getUserName();
		if (StringUtils.isNotEmpty(msg)) {
			result.append(msg);
			result.append("\r\n");
			result.append("                   ").append(userName).append(" ")
					.append(createTime);
		}
		return result.toString();
	}
}
